package pt.fct.mpc.lastfm.search;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import pt.fct.mpc.lastfm.R;
import pt.fct.mpc.lastfm.connectivity.LastfmBluetoothUnavailableException;
import pt.fct.mpc.lastfm.connectivity.LastfmURL;
import pt.fct.mpc.lastfm.event.EventData;
import pt.fct.mpc.lastfm.event.EventsXmlHandler;
import android.content.Context;
import android.location.Address;
import android.util.Log;

import com.google.android.maps.GeoPoint;

public class LastfmSearchEvents
{
	private static final String TAG = "Last.fm Search Events";
	
	private static final int SEARCH_EVENTS_LIMIT = 10;
	
	@SuppressWarnings("unused")
	private Context context;

	private String lastfm_api_url;
	private String lastfm_api_key;

	
	public LastfmSearchEvents(Context context)
	{
		this.context = context;
		
		this.lastfm_api_url = context.getString(R.string.lastfm_api_url);
		this.lastfm_api_key = context.getString(R.string.lastfm_api_key);
	}
	
	public EventData[] getEvents(Address locationAddress) 
		throws LastfmBluetoothUnavailableException
	{
		if (locationAddress == null)
			return new EventData[0];
		
		String locality = locationAddress.getLocality();
		Log.d(TAG, "Location address retrived: "+locality);
		
		return this.getEvents("&location="+locality);
	}
	
	public EventData[] getEvents(GeoPoint lastKnownLocation) 
		throws LastfmBluetoothUnavailableException
	{
		if (lastKnownLocation == null)
			return new EventData[0];

		double lat = lastKnownLocation.getLatitudeE6() / 1E6;
		double lng = lastKnownLocation.getLongitudeE6() / 1E6;

		String location_str = "&lat="+lat+"&long="+lng;
		
		return this.getEvents(location_str);
	}

	public EventData[] getEvents(String location_str) 
		throws LastfmBluetoothUnavailableException
	{
		EventData[] results = null;
		
		String search_params =
			"?method=geo.getevents"
			+location_str
			+"&api_key="+lastfm_api_key
			+"&limit="+SEARCH_EVENTS_LIMIT;
		
		String search_url = lastfm_api_url+search_params;
		
		Log.d(TAG, "request URL = "+search_url);
		
		LastfmURL request_url = null;
		try
		{
			request_url = new LastfmURL(search_url);
			
		} catch (MalformedURLException e)
		{
			e.printStackTrace();
			return null;
		}
		
		SAXParserFactory spf = SAXParserFactory.newInstance();
		SAXParser sp = null;
		try 
		{
			sp = spf.newSAXParser();
		} catch (ParserConfigurationException e) 
		{
			e.printStackTrace();
			return null;
		} catch (SAXException e) 
		{
			e.printStackTrace();
			return null;
		} 

		XMLReader xr = null;
		try {
			xr = sp.getXMLReader();
		} catch (SAXException e) {
			e.printStackTrace();
			return null;
		} 

		EventsXmlHandler  myEventsXmlHandler = new EventsXmlHandler();
		xr.setContentHandler(myEventsXmlHandler); 

		try {
			xr.parse(new InputSource(request_url.openStream()));
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} catch (SAXException e) {
			e.printStackTrace();
			return null;
		} 

		List<EventData> event_data = myEventsXmlHandler.getParsedData();
		
		if (event_data != null)
			Log.d(TAG, "total events in list = "+event_data.size());
		else
			Log.e(TAG, "event data list is null");
		
		if (event_data != null)
			results = event_data.toArray(new EventData[event_data.size()]);
		
		return results;
	}

}
